package com.yiren.dbaa.module.practice_set.site.view.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.lljjcoder.Interface.OnCustomCityPickerItemClickListener;
import com.lljjcoder.bean.CustomCityData;
import com.lljjcoder.style.citycustome.CustomCityPicker;
import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.UserParamConfigBean;
import com.yiren.backstage.currency.bean.GpsInfoBean;
import com.yiren.backstage.currency.entity.SiteEntity;
import com.yiren.backstage.currency.utils.StringUtil;
import com.yiren.dbaa.R;
import com.yiren.dbaa.activity.BaseActivity;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.practice_set.site.bean.BaseRuleNewBean;
import com.yiren.dbaa.module.practice_set.site.bean.CityBean;
import com.yiren.dbaa.module.practice_set.site.bean.CompanyBean;
import com.yiren.dbaa.module.practice_set.site.contract.AddSiteRuleInfoContract;
import com.yiren.dbaa.module.practice_set.site.presenter.AddSiteRuleInfoPresenter;
import com.yiren.dbaa.module.practice_set.site.view.adapter.CitySelectorView;
import com.yiren.dbaa.view.toast.MessageBox;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lwb
 * @date : 2022/9/26 8:48
 * @description: 新建规则信息详情页
 */
public class AddRuleInfoActivity extends BaseActivity implements AddSiteRuleInfoContract.View{

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.sp_siterule_source)
    Spinner spSiteruleSource;
    @BindView(R.id.tv_siterule_source)
    TextView tvSiteruleSource;
    @BindView(R.id.tv_city)
    TextView tvCity;
    @BindView(R.id.sp_site_name)
    Spinner spSiteName;
    @BindView(R.id.ll_site_name)
    LinearLayout llSiteName;
    @BindView(R.id.et_site_name)
    EditText edSiteName;
    @BindView(R.id.sp_site_system)
    Spinner spSiteSystem;
    @BindView(R.id.tv_company)
    TextView tvCompany;

    private AddSiteRuleInfoContract.Presenter addSiteRuleInfoPresenter;
    private UserParamConfigBean userParamConfigBean;
    private CustomCityPicker customCityPicker;
    /**是否选中新建*/
    private boolean isSelectedAdd = false;
    /**选中的城市*/
    private CityBean  city;
    /**已公开考规集合*/
    private List<SiteEntity> serviceSites;
    /**考场规则数据来源对象*/
    private SiteEntity siteRulesource;
    /**新建考规对象*/
    private SiteEntity newSite;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addrule_info);
        ButterKnife.bind(this);
        tvTitle.setText("新建考场规则");
        this.city = (CityBean) getIntent().getSerializableExtra("city");
        this.siteRulesource = (SiteEntity) getIntent().getSerializableExtra("site");
        if (null != siteRulesource) isSelectedAdd = true;
        init();
    }

    private void init() {
        userParamConfigBean = LoadConfigApi.loadUserParamConfig(this);

        addSiteRuleInfoPresenter = new AddSiteRuleInfoPresenter(this);
        addSiteRuleInfoPresenter.loadAllCompany(this);
        addSiteRuleInfoPresenter.loadSiteRuleFromServer(this);

        customCityPicker = CitySelectorView.createView(this);
        customCityPicker.setOnCustomCityPickerItemClickListener(new OnCustomCityPickerItemClickListener() {
            @Override
            public void onSelected(CustomCityData province, CustomCityData city, CustomCityData district) {
                tvCity.setText(city.getName());
                addSiteRuleInfoPresenter.loadCitySiteName(city.getId(),AddRuleInfoActivity.this);
            }
        });

        if(isSelectedAdd){
            //选中新建
            tvSiteruleSource.setVisibility(View.VISIBLE);
            spSiteruleSource.setVisibility(View.GONE);
            tvSiteruleSource.setText(siteRulesource.getSiteName()+"-"+siteRulesource.getUserId());
            if (city!=null){
                tvCity.setText(city.getName());
                tvCompany.setVisibility(View.VISIBLE);
                spSiteSystem.setVisibility(View.GONE);
                tvCompany.setText(siteRulesource.getC_name());
                addSiteRuleInfoPresenter.loadCitySiteName(city.getId(),this);
            }
        }else{
            //自己新建
            tvSiteruleSource.setVisibility(View.GONE);
            spSiteruleSource.setVisibility(View.VISIBLE);
            String city = GpsInfoBean.getInstance().getCity();
            if (!StringUtil.isEmpty(city)) {
                tvCity.setText(city);
                addSiteRuleInfoPresenter.loadCitySiteName(GpsInfoBean.getInstance().getCityId(),AddRuleInfoActivity.this);
            } else {
                tvCity.setText("请选择城市");
                MessageBox.show(this, "定位失败!请手动选择城市!");
            }
        }
    }

    @OnClick({R.id.iv_back, R.id.tv_city,R.id.rl_add_siterule})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_city:
              if (customCityPicker!=null)
                  customCityPicker.showCityPicker();
                break;
            case R.id.rl_add_siterule:
                if(tvCity.getText().equals("请选择城市")){
                    MessageBox.show(this, "请选择城市");
                    return;
                };
                if(null == newSite){
                    MessageBox.show(this,"当前城市下考场列表为空，请联系专属客服！");
                    return;
                }

                if (isSelectedAdd){
                    addSiteRuleInfoPresenter.saveSiteRule(serviceSites,siteRulesource,newSite);
                }else {
                    siteRulesource.setUserId(userParamConfigBean.getUserInfo().getId().toString());
                    addSiteRuleInfoPresenter.saveSiteRule(serviceSites,siteRulesource,newSite);
                }
                break;
        }
    }

    @Override
    public void showSiteRuleSource(List<SiteEntity> siteSourceList) {
        this.serviceSites = siteSourceList;
        if(isSelectedAdd) return;
        //自己新建-考规来源数据绑定
        List<String> siteSourceNameList = new ArrayList<>();
        siteSourceNameList.add("基础模板(官方数据)");
        for (int i = 0;i< siteSourceList.size();i++) {
            siteSourceNameList.add(siteSourceList.get(i).getSiteName()+"(个人数据)");
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.item_spinner,siteSourceNameList);
        adapter.setDropDownViewResource(R.layout.item_spinner_dropdown);
        spSiteruleSource.setAdapter(adapter);
        spSiteruleSource.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0){
                    //自己新建 选择基础模板数据
                    siteRulesource = new SiteEntity();
                    addSiteRuleInfoPresenter.loadNewBaseRule(new CommonCallBack() {
                        @Override
                        public void success(Object result) {
                            List<BaseRuleNewBean> baseRuleNewBean = (List<BaseRuleNewBean>) result;
                            siteRulesource.setBaseVer(baseRuleNewBean.get(position).getVer());
                        }
                    });
                }else {
                    siteRulesource = siteSourceList.get(position-1);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    @Override
    public void showSiteName(List<SiteEntity> citySiteList) {
        //默认选项
        int defaultIndex = 0;
        List<String> citySiteNameList = new ArrayList<>();
        for (int i = 0;i< citySiteList.size();i++){
            citySiteNameList.add(citySiteList.get(i).getSiteName());
            if (isSelectedAdd) {
                if (siteRulesource.getSiteCode().equals(citySiteList.get(i).getSiteCode())) {
                    defaultIndex = i;
                }
            }
        }
        //citySiteNameList.add("自定义");
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.item_spinner,citySiteNameList);
        adapter.setDropDownViewResource(R.layout.item_spinner_dropdown);
        spSiteName.setAdapter(adapter);
        if (citySiteList.size() == 0){
           /* spSiteName.setSelection(citySiteNameList.size()-1);
            llSiteName.setVisibility(View.VISIBLE);
            tvCompany.setVisibility(View.GONE);
            spSiteSystem.setVisibility(View.VISIBLE);*/
            newSite = null;
            MessageBox.show(this,"当前城市下考场列表为空，请联系专属客服提交！");
        }else {
            spSiteName.setSelection(defaultIndex);
            newSite = citySiteList.get(defaultIndex);
        }
        spSiteName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
              /*  if (position == citySiteNameList.size()-1){
                    llSiteName.setVisibility(View.VISIBLE);
                    tvCompany.setVisibility(View.GONE);
                    spSiteSystem.setVisibility(View.VISIBLE);
                    siteName = edSiteName.getText().toString();
                }else {*/
                    llSiteName.setVisibility(View.GONE);
                    tvCompany.setVisibility(View.VISIBLE);
                    spSiteSystem.setVisibility(View.GONE);
                    tvCompany.setText(citySiteList.get(position).getC_name());
                    newSite = citySiteList.get(position);
                //}
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    @Override
    public void showCompany(List<CompanyBean> companyList) {
        List<String> companyNameList = new ArrayList<>();
        for (CompanyBean company:companyList){
            companyNameList.add(company.getC_name());
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.item_spinner, companyNameList);
        adapter.setDropDownViewResource(R.layout.item_spinner_dropdown);
        spSiteSystem.setAdapter(adapter);
        spSiteSystem.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(null != newSite) newSite.setC_name(companyNameList.get(position));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

}